<?php

/*

  Harley 2016-5-17

   */
include_once ('Amazon_config.inc.php');
require_once ("Harley_DBF.php");
require_once ("Utils.php");
require_once ("Global_Config.php");
require_once ("String.class.php");
require_once ("Des.class.php");
require_once ("UserAuth.php");

header('Access-Control-Allow-Origin:*');

//error_reporting(E_ALL);
//ini_set("display_errors","off");


$type = $_SERVER['REQUEST_METHOD'];
parse_str(file_get_contents('php://input'), $data);
$data = array_merge($_GET, $_POST, $data);
$data2 = $data['data'];
$arr = json_decode($data2,true);
extract($arr,true);


$UserID_value = isset($UserID)?$UserID:"";

$UserName_value = isset($UserName)?$UserName:"";
$PasswordMD5_value = isset($PasswordMD5)?$PasswordMD5:"";

$BangTuSecret_value = isset($BangTuSecret)?$BangTuSecret:"";

//$ServiceURL_value = isset($ServiceURL)?$ServiceURL:"";

$MarketID_value = isset($MarketID)?$MarketID:"";

//------------test begin ------------------------------

$UserID_value = isset($UserID)?$UserID:"100";

$UserName_value = isset($UserName)?$UserName:"125917130@qq.com";
$PasswordMD5_value = isset($PasswordMD5)?$PasswordMD5:"b4c746fd0b836035a52c797d78429028";

$BangTuSecret_value = isset($BangTuSecret)?$BangTuSecret:"";

//$ServiceURL_value = isset($ServiceURL)?$ServiceURL:"";

$MarketID_value = isset($MarketID)?$MarketID:"2";

$sqlArr = array('AND'=>array('UserID[=]'=>$UserID_value),'MarketID[=]'=>$MarketID_value) ;

// 筛选 按 产品 状态
if(isset($ProductStatus)){
    $sqlArr['AND']['ProductStatus[=]'] = $ProductStatus;
}

// 筛选 按 产品  数量
if(isset($Quantity)){
    $sqlArr['AND']['Quantity[=]'] = $Quantity;
}

// 筛选 按 BuyBox 价格
if(isset($BuyBoxPrice)){
    $sqlArr['AND']['BuyBoxPrice[=]'] = $BuyBoxPrice;
}

// 筛选 按 BuyBox 发货类型
if(isset($DespatchModeType)){
    $sqlArr['AND']['DespatchModeType[=]'] = $DespatchModeType;
}

// 筛选 按ASIN / SellerSKU
if(isset($ASINorSellerSKU)){
    $sqlArr['AND']['#ASIN[=]'] = "ASIN '%".$ASINorSellerSKU."%' OR SellerSKU '%".$ASINorSellerSKU."%'";
}

//------------test End ------------------------------

//echo "here!!";
//检测是否对应的用户

$user_auth = new UserAuth();

if(!$user_auth->CheckUserAuth($UserName_value, $PasswordMD5_value))
{
    $respon = array('code' => '400',
        'msg' => '授权错误，请检测对应的账号与密码字段！'
    );
    echo JSON($respon);
    exit(0);
}

$database = new Harley_DBF([
    // required
    'database_type' => 'mysql',
    'database_name' => DATABASE_NAME,
    'server' => DATABASE_SERVER,
    'username' => DATABASE_USERNAME,
    'password' => DATABASE_PASSWORD,
    'charset' => 'utf8',

    // [optional]
    'port' => 3306,

    // [optional] Table prefix
    'prefix' => DATABASE_PREFIX,

    // driver_option for connection, read more from http://www.php.net/manual/en/pdo.setattribute.php
    'option' => [
        PDO::ATTR_CASE => PDO::CASE_NATURAL
    ]
]);


$tableName = "reportprocessinfo"; // 记住不用加 opolo_ 前缀了

$datas = [];

$datas = $database->select($tableName, "*",
    ["AND" =>[
        "UserID[=]" => $UserID_value,
        "MarketID[=]" => $MarketID_value,
        "#CreateTime[=]" => "to_days(date_format(from_UNIXTIME(`createtime`),'%Y-%m-%d')) = to_days(now());",
    ]]);




$tableName = "productqtypricetemp"; // 记住不用加 opolo_ 前缀了

$datas = [];




if (''=== $UserID_value && ''=== $PasswordMD5_value && ''===$BangTuSecret_value)
{


    $respon = array('code' => '200',
        'msg' => 'Error,Need some info,Please contact Harley!!!',
        "values" => ""
    );

}
else
{
//    $datas = $database->select($tableName, "*",
//        ["AND" =>[
//            "UserID[=]" => $UserID_value,
//            "MarketID[=]" => $MarketID_value
//        ]]);

     $datas = $database->select($tableName, "*",$sqlArr);


    //var_dump($datas);
    //echo "2222 here";

    if(count($datas)>0)
    {

        $respon = array('code' => '200',
            'msg' => 'Success',
            "values" => $datas
        );

    }
    else
    {
        $url = POST_URL;

        $post_data['UserID'] =$UserID_value;

        $post_data['PasswordMD5'] = $PasswordMD5_value;

        $post_data['UserName'] =  $UserName_value;

        $post_data['BangTuSecret'] =  $BangTuSecret_value;

        $post_data['MarketID'] =  $MarketID_value;

        $data['data'] = json_encode($post_data);

        request_post_no_wait($url, $data);

        $respon = array('code' => '202',
            'msg' => '正在初始化数据中……'

        );
    }



}

echo JSON($respon);


/**
 * 模拟post进行url请求
 * @param string $url
 * @param array $post_data
 * @author xiaojing
 */
function request_post_no_wait($url = '', $post_data = array())
{

    $o = "";
    foreach ( $post_data as $k => $v )
    {
        $o.= "$k=" . urlencode( $v ). "&" ;
    }
    $post_data = substr($o,0,-1);

    $postUrl = $url;
    $curlPost = $post_data;

    var_dump($curlPost);
    echo $postUrl;

//    $ch = curl_init();//初始化curl
//    curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
//    curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
//    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
//    curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
//    curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $postUrl);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
    curl_setopt($ch, CURLOPT_TIMEOUT, 2);//1秒后立即执行
    curl_exec($ch);
    curl_close($ch);


//    $data = curl_exec($ch);//运行curl
//    curl_close($ch);
//    return $data;
}

?>

                                                                                
